/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.AtividadeDao;
import Dao.AtividadeDaoImplJPA;
import Dao.TurmaDao;
import Dao.TurmaDaoImplJPA;
import Pojo.Atividade;
import Pojo.Professor;
import Pojo.Turma;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author thiago
 */
public class FormConsultaAtividades extends javax.swing.JInternalFrame {
    /**
     * Acesso necessário para o caso de chamar o cadastro de professore
     */
    private int acesso;
    
    /**
     * Objeto professor para acesso.
     */
    private Professor professorAcesso;
    
    /**
     * Getter do acesso
     * @return 
     */
    public int getAcesso() {
        return acesso;
    }
    
    /**
     * Setter de acesso
     * @param acesso 
     */
    private void setAcesso(int acesso){
        this.acesso = acesso;
    }

    public Professor getProfessorAcesso() {
        return professorAcesso;
    }

    private void setProfessorAcesso(Professor professorAcesso) {
        this.professorAcesso = professorAcesso;
    }
    
    /**
     * Creates new form FormConsultaAtividades
     */
    public FormConsultaAtividades(int acesso, Professor professor) {
        initComponents();
        this.buscarTurmas();
        this.setAcesso(acesso);
        this.setProfessorAcesso(professor);
        lblProfessor.setText(this.getProfessorAcesso().getNome());
        this.buscarTurmas();
    }

    private void buscarTurmas(){
        TurmaDao turmaDao = new TurmaDaoImplJPA();
        List<Turma> listaTurmas = turmaDao.pesquisarPorProfessor(this.getProfessorAcesso());
        for(Turma turma : listaTurmas){
            cbbTurmas.addItem(turma.getId()+" - "+turma.getDisciplina().getNome()+" - "+turma.getAno()+" - "+turma.getPeriodo());
        }
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        lblProfessor = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbbTurmas = new javax.swing.JComboBox();
        btnPesquisar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblAtividades = new javax.swing.JTable();

        setClosable(true);
        setTitle("Consulta de Atividades");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisar"));

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 13)); // NOI18N
        jLabel1.setText("Professor");

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 13)); // NOI18N
        jLabel2.setText("Turma");

        btnPesquisar.setText("Pesquisar");
        btnPesquisar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cbbTurmas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 96, Short.MAX_VALUE)
                        .addComponent(btnPesquisar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cbbTurmas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(lblProfessor, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnPesquisar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tblAtividades.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "Tipo", "Data", "Valor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblAtividades);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarActionPerformed
        String turmaString = (String) cbbTurmas.getSelectedItem();
        String[] dados;
        if (turmaString.length()>0){
            dados = turmaString.split(" - ");
            TurmaDao turmaDao = new TurmaDaoImplJPA();
            Turma turma = turmaDao.pesquisarPorId(Integer.parseInt(dados[0]));
            
            AtividadeDao atividadeDao = new AtividadeDaoImplJPA();
            List<Atividade> listaAtividades = atividadeDao.pesquisarPorTurma(turma);
            
            DefaultTableModel model = (DefaultTableModel) tblAtividades.getModel();
            Object[] linha = new Object[5];
            model.setRowCount(0);
            for (Object o: listaAtividades){
                Atividade a = (Atividade) o;
                linha[0] = String.valueOf(a.getId());
                linha[1] = a.getNome();
                linha[2] = a.getTipo();
                DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
                String dataString; 
                try{
                    dataString = formatter.format(a.getData().getTime()); 
                }catch(Exception e){
                    System.err.print(e);
                    dataString = null;
                }
                linha[3] = dataString;
                linha[4] = a.getValor();
                model.addRow(linha);
            }
            tblAtividades.repaint();
        }
    }//GEN-LAST:event_btnPesquisarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisar;
    private javax.swing.JComboBox cbbTurmas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblProfessor;
    private javax.swing.JTable tblAtividades;
    // End of variables declaration//GEN-END:variables
}
